<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="inc/taglibs.jsp" %>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <%@ include file="inc/meta.jsp" %>
    <meta name="description" content="">
    <meta name="author" content="">
    <title>收信人列表</title>
    <%@ include file="inc/css.jsp" %>
</head>

<body>
<div id="posts" class="wrapper">

    <%@ include file="inc/nav.jsp" %>

    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">信件列表</h1>
                <h4 style="margin-left: 10px;">——收信人列表</h4>
            </div>
            <!-- /.col-lg-12 -->
        </div>

        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <input type="hidden" id="letterId" value="${letterId}"/>
                        <a href="javascript:void(0)" onclick="_letterAcceptList.fn.batchDel()" class="btn btn-outline btn-danger btn-lg" role="button">一键删除</a>
                        <a href="backend/letter/add?letterId=${letterId}" class="btn btn-outline btn-info btn-lg" role="button">查看信件详情</a>
                        <a href="backend/letter/index" class="btn btn-outline btn-success btn-lg" role="button">返回列表</a>
                    </div>
                    <!-- /.panel-heading -->
                    <div class="panel-body">
                        <div class="table-responsive">
                            <table class="table table-striped table-bordered table-hover" id="dataTables">
                                <colgroup>
                                    <col class="gradeA odd" style="width: 6%"/>
                                    <col class="gradeA even"/>
                                    <col class="gradeA odd"/>
                                    <col class="gradeA even"/>
                                    <col class="gradeA odd"/>
                                </colgroup>
                                <thead>
                                <tr>
                                    <th><input type="checkbox" onclick="$leoman.checkAll(this)" class="checkall"/></th>
                                    <th>用户昵称</th>
                                    <th>收信时间</th>
                                    <th>物流状态</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>
                        </div>

                    </div>
                    <!-- /.panel-body -->

                </div>
                <!-- /.panel -->
            </div>
        </div>
    </div>
    <!-- /#page-wrapper -->

    <!-- Modal -->
    <div class="modal fade" id="expressModal" tabindex="-1" role="dialog" aria-labelledby="pwdModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title">物流信息</h4>
                </div>
                <div class="modal-body">
                    <form id="expressForm" method="post" action="backend/letter/accept/save" class="form-horizontal nice-validator n-default" role="form" novalidate="novalidate">
                        <input type="hidden" name="letterAcceptId" id="tempLetterAcceptId"/>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">快递公司:</label>

                            <div class="col-sm-8">
                                <input type="text" class="form-control" id="expressName" name="expressName" maxlength="20" placeholder="请输入快递公司"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">快递单号:</label>

                            <div class="col-sm-8">
                                <input type="text" class="form-control" id="expressNum" name="expressNum" maxlength="20" placeholder="请输入快递单号"/>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" onclick="_letterAcceptList.fn.subInfo()" class="btn btn-primary">确定</button>
                </div>
            </div>
        </div>
    </div>

</div>
<!-- /#wrapper -->

<%@ include file="inc/footer.jsp" %>
</body>

<script type="text/javascript">
    var _letterAcceptList = {
        v: {
            id: "_letterAcceptList",
            list: [],
            dTable: null
        },
        fn: {
            init: function () {
                _letterAcceptList.fn.dataTableInit();
            },
            dataTableInit: function () {
                _letterAcceptList.v.dTable = $leoman.dataTable($('#dataTables'), {
                    "processing": true,
                    "serverSide": true,
                    "searching": false,
                    "ordering": false,
                    "ajax": {
                        "url": "backend/letter/accept/list",
                        "type": "POST"
                    },
                    "columns": [
                        {"data": "id"},
                        {"data": "user.nickName"},
                        {
                            "data": "createTime",
                            render: function (data) {
                                return formatJsonDateTime(data, 'yyyy-MM-dd hh:mm:ss');
                            }
                        },
                        {
                            "data": "expressName",
                            render: function (data) {
                                if (null != data && data != '') {
                                    return "已发";
                                } else {
                                    return "未发";
                                }
                            }
                        },
                        {"data": ""}
                    ],
                    "columnDefs": [
                        {
                            "data": null,
                            "defaultContent": "<button type='button' title='查看' class='btn btn-primary btn-circle look'>" +
                            "<i class='fa fa-eye'></i>" +
                            "</button>" +
                            "<button type='button' title='打印信件' style='margin-left: 8px;display: none' class='btn btn-info btn-circle printLetter'>" +
                            "<i class='fa fa-print'></i>" +
                            "</button>" +
                            "<button type='button' title='编辑物流' style='margin-left: 8px;display: none' class='btn btn-warning btn-circle editExpress'>" +
                            "<i class='fa fa-edit'></i>" +
                            "</button>",
                            "targets": -1
                        }
                    ],
                    "createdRow": function (row, data, index) {
                        _letterAcceptList.v.list.push(data);
                        $('td', row).eq(0).html("<input type='checkbox' value=" + data.id + ">");
                    },
                    rowCallback: function (row, data) {
                        if (data.printStatus == 0) {
                            $('td', row).last().find(".printLetter").css('display', '');
                        }

                        if (null == data.expressName || data.expressName == '') {
                            $('td', row).last().find(".editExpress").css('display', '');
                        }

                        $('td', row).last().find(".look").click(function () {
                            window.location.href = 'backend/letter/accept/info?letterAcceptId=' + data.id;
                        });

                        $('td', row).last().find(".printLetter").click(function () {
                            _letterAcceptList.fn.modifyPrintStatus(data.id);
                        });

                        $('td', row).last().find(".editExpress").click(function () {
                            _letterAcceptList.fn.editExpressInfo(data.id);
                        });
                    },
                    "fnServerParams": function (aoData) {
                        aoData.letterId = $('#letterId').val();
                    },
                    "fnDrawCallback": function (row) {
                        $leoman.uiform();
                    }
                });
            },
            batchDel: function () {
                var checkBox = $("#dataTables tbody tr").find('input[type=checkbox]:checked');
                var ids = checkBox.getInputId();
                _letterAcceptList.fn.deleteRow(checkBox, ids)
            },
            deleteRow: function (checkBox, ids) {
                if (ids.length > 0) {
                    $leoman.optNotify(function () {
                        $leoman.ajax("backend/letter/accept/batchDelete", {
                            ids: JSON.stringify(ids)
                        }, function (result) {
                            if (result > 0) {
                                $leoman.notify("操作成功", "success");
                                _letterAcceptList.v.dTable.ajax.reload();
                            } else {
                                $leoman.notify("操作失败", "error");
                            }
                        })
                    }, '确定删除选中的所有收信人信息？', '确定');
                }
            },
            modifyPrintStatus: function (id) {
                $.ajax({
                    url: _basePath + "backend/letter/modifyPrintStatus",
                    dataType: "json",
                    data: {
                        id: id,
                        type: 2
                    },
                    success: function (data) {
                        if (data == 1) {
                            $leoman.notify("操作成功", "success");
                            _letterAcceptList.v.dTable.ajax.reload();
                        } else {
                            $leoman.notify("操作失败", "error");
                        }
                    }
                });
            },
            editExpressInfo: function (id) {
                $('#tempLetterAcceptId').val(id);
                $('#expressName').val('');
                $('#expressNum').val('');
                $('#expressModal').modal('show');
            },
            subInfo: function () {
                var flag = true;
                var expressName = $('#expressName').val();
                var expressNum = $('#expressNum').val();

                if (null == expressName || expressName == '') {
                    $leoman.notify('快递公司名称不能为空', "error");
                    flag = false;
                    return;
                }

                if (null == expressNum || expressNum == '') {
                    $leoman.notify('快递单号不能为空', "error");
                    flag = false;
                    return;
                }

                // 所有的验证通过后，执行新增操作
                if (flag) {
                    $("#expressForm").ajaxSubmit({
                        url: _basePath + "backend/letter/accept/save",
                        dataType: "json",
                        success: function (data) {
                            if (data == 1) {
                                $('#expressModal').modal('hide');
                                $leoman.notify("操作成功", "success");
                                _letterAcceptList.v.dTable.ajax.reload();
                            } else {
                                $leoman.notify("操作失败", "error");
                            }
                        }
                    });
                }
            }
        }
    }

    $(document).ready(function () {
        _letterAcceptList.fn.init();
    });

</script>

</html>